#include "getconnectconfig.h"

getconnectconfig::getconnectconfig(QObject *parent) : QObject(parent)
{
//    config_init();
}

void getconnectconfig::config_init()
{

}

QString getconnectconfig::get_connectconf_value(QString setction, QString key)
{
    QSettings settings(PROFILE_CONF_PATH, QSettings::IniFormat);
//    qDebug()<<"<"<<setction<<"/"<<key<<">"<<" :"<<settings.value(setction+"/"+key).toString();
    return settings.value(setction+"/"+key).toString();
}

void getconnectconfig::set_connectconf_value(QString setction, QString key, QString value)
{
    QSettings settings(DEFAULT_CONF_USER_PATH, QSettings::IniFormat);
    QByteArray ba;
    ba = value.toLatin1();
    settings.setValue(setction+"/"+key,  ba.toBase64());
    settings.sync();
    qInfo()<<"Change <"<<setction<<"/"<<key<<">"<<" to :"<<value;
}

bool getconnectconfig::get_current_active_state()
{
    QString config_path = QDir::homePath() + "/.config/activation_conf.ini";
    QFileInfo file(config_path);
    QString current_active;
    bool check_state = false;
    if (!file.exists()) {
        qDebug() << "activation_conf.ini isn't exist.";
    }
    QSettings settings(config_path, QSettings::IniFormat);
    current_active = settings.value("Active_State/current_active").toString();
    if (current_active=="true") {
        check_state = true;
        qDebug() << "Current active state: true";
    } else if (current_active=="false") {
        qDebug() << "Current active state: false";
    } else {
        qDebug() << "Activation status not obtained ...";
        qDebug() <<"current_active: "<<current_active;
    }
    return check_state;
}

int getconnectconfig::generate_random_time(int initial_interval, int offset_value)
{
    int strayParameter;
    assert((offset_value>0)&&(initial_interval>=0));
    qsrand(QRandomGenerator::global()->generate());  //随机数种子
    strayParameter = (qrand()%(offset_value));
//    qDebug()<<"strayParameter: "<<strayParameter<<" ,random: "<<strayParameter+initial_interval;
    qInfo()<<"strayParameter: "<<strayParameter<<" ,random: "<<strayParameter+initial_interval;
    return strayParameter+initial_interval;
}

QString getconnectconfig::get_defaultconf_value(QString setction, QString key)
{
    QSettings settings(DEFAULT_CONF_PATH, QSettings::IniFormat);
//    qDebug()<<"<"<<setction<<"/"<<key<<">"<<" :"<<settings.value(setction+"/"+key).toString();
    return settings.value(setction+"/"+key).toString();
}
